package com.duguo.dynamicmvc.factory;

import java.util.Map;

import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.FactoryBean;

import com.duguo.dynamicmvc.factory.interceptor.InterceptorGenerator;


public abstract class AbstractControllerFactory implements ControllerFactory, FactoryBean, BeanNameAware {

    private Class controllerClass;
    protected Object controllerInstance;
    private Class dynamicViewModelClass;
    private Map<String,InterceptorGenerator> interceptorGenerators;
    private Map<String,Object> injections;
    protected WorkingModel workingModel = new WorkingModel(this);

    public Object getObject() throws Exception {
        if (controllerInstance == null)
        	workingModel.getControllerBuilder().build();
        return controllerInstance;
    }

    /*
      * delegate setters
      */

    public void setControllerClass(Class controllerClass) {
        this.controllerClass = controllerClass;
        workingModel.setControllerClass(controllerClass);
    }

    public void setControllerBuilder(ControllerBuilder controllerBuilder) {
        workingModel.setControllerBuilder(controllerBuilder);
    }

    public void setStaticViewMapping(Map<String, Object> staticViewMapping) {
        workingModel.setStaticViewMapping(staticViewMapping);
    }

    public void setParameterResolvers(Map<String,Object> parameterResolvers) {
        workingModel.setParameterResolvers(parameterResolvers);
    }

    public void setParameterValidators(Map<String,Object> parameterValidators) {
        workingModel.setParameterValidators(parameterValidators);
    }
    
    public void setBeanName(String name) {
        if(!name.endsWith("/")){
        	throw new RuntimeException("Controller path must be end with /. Current controller path is "+name);
        }
        workingModel.setControllerPath(name);
    }

    /*
      * delegate getters
      */

    public ControllerBuilder getControllerBuilder() {
        return workingModel.getControllerBuilder();
    }

    public String getControllerPath() {
        return workingModel.getControllerPath();
    }

    /*
      * general setters
      */

    public void setControllerInstance(Object controllerInstance) {
        this.controllerInstance = controllerInstance;
    }

    public void setWorkingModel(WorkingModel workingModel) {
        this.workingModel = workingModel;
    }

    /*
      * general getters
      */

    public boolean isSingleton() {
        return true;
    }

    public Class getObjectType() {
        return controllerClass;
    }

    public WorkingModel getWorkingModel() {
        return workingModel;
    }

	public Class getDynamicViewModelClass() {
		return dynamicViewModelClass;
	}

	public void setDynamicViewModelClass(Class dynamicViewModelClass) {
		this.dynamicViewModelClass = dynamicViewModelClass;
	}

	public Map<String,InterceptorGenerator> getInterceptorGenerators() {
		return interceptorGenerators;
	}

	public void setInterceptorGenerators(
			Map<String,InterceptorGenerator> interceptorGenerators) {
		this.interceptorGenerators = interceptorGenerators;
	}

	public Map<String, Object> getInjections() {
		return injections;
	}

	public void setInjections(Map<String, Object> injections) {
		this.injections = injections;
	}


}
